<?php
namespace app\index\controller;


use app\common\models\Carousel;
use app\common\models\Page;
use app\index\validate\MessageValidate;

class Index extends Base
{

    public function index()
    {
        //获取轮播图
        $carousel = new Carousel();
        $list = $carousel->getMany();
        $productModel = new \app\common\models\Product();
        $product1 = $productModel->getPageData(['status'=>1,'category_id'=>1])->toArray();
        $product2 = $productModel->getPageData(['status'=>1,'category_id'=>2])->toArray();
        return view('',['list'=>$list,'product1'=>$product1,'product2'=>$product2]);
    }

    public function about(){

        $page = new Page();
        $data = $page->getOne(['status'=>1,'mark'=>'about']);
        if (is_null($data)){
            $data = ['title'=>'','content'=>''];
        }
        return view('',['data'=>$data]);
    }

    public function contact(){

        $page = new Page();
        $data = $page->getOne(['status'=>1,'mark'=>'contact']);
        if (is_null($data)){
            $data = ['title'=>'','content'=>''];
        }
        return view('',['data'=>$data]);
    }
    
    public function message(){
        $data = $this->request->post();
        $validate = new MessageValidate();
        if (!$validate->check($data)){
            $this->error($validate->getError());
        }
        $messageBoard = new \app\common\models\MessageBoard();
        $add = $messageBoard->addDataByField($data);
        if ($add){
            $this->success('留言成功');
        }else{
            $this->error('留言失败');
        }

    }
}
